package org.jivesoftware.smack;

import java.util.Collection;
import java.util.Collections;
import java.util.Iterator;
import java.util.Map;
import java.util.Map.Entry;
import java.util.Set;
import java.util.WeakHashMap;
import java.util.concurrent.CopyOnWriteArraySet;
import org.jivesoftware.smack.filter.AndFilter;
import org.jivesoftware.smack.filter.FromContainsFilter;
import org.jivesoftware.smack.filter.PacketFilter;
import org.jivesoftware.smack.filter.ThreadFilter;
import org.jivesoftware.smack.packet.Message;
import org.jivesoftware.smack.packet.Message.Type;
import org.jivesoftware.smack.packet.Packet;
import org.jivesoftware.smack.util.StringUtils;
import org.jivesoftware.smack.util.collections.ReferenceMap;

public class ChatManager
{
  private static String prefix = StringUtils.randomString(5);
  private static long id = 0L;
  private Map<String, Chat> threadChats = Collections.synchronizedMap(new ReferenceMap(0, 2));
  private Map<String, Chat> jidChats = Collections.synchronizedMap(new ReferenceMap(0, 2));
  private Map<String, Chat> baseJidChats = Collections.synchronizedMap(new ReferenceMap(0, 2));
  private Set<ChatManagerListener> chatManagerListeners = new CopyOnWriteArraySet();
  private Map<PacketInterceptor, PacketFilter> interceptors = new WeakHashMap();
  private Connection connection;

  private static synchronized String nextID()
  {
    return prefix + Long.toString(id++);
  }

  ChatManager(Connection paramConnection)
  {
    this.connection = paramConnection;
    PacketFilter local1 = new PacketFilter()
    {
      public boolean accept(Packet paramAnonymousPacket)
      {
        if (!(paramAnonymousPacket instanceof Message))
          return false;
        Message.Type localType = ((Message)paramAnonymousPacket).getType();
        return (localType != Message.Type.groupchat) && (localType != Message.Type.headline);
      }
    };
    paramConnection.addPacketListener(new PacketListener()
    {
      public void processPacket(Packet paramAnonymousPacket)
      {
        Message localMessage = (Message)paramAnonymousPacket;
        Chat localChat;
        if (localMessage.getThread() == null)
        {
          localChat = ChatManager.this.getUserChat(localMessage.getFrom());
        }
        else
        {
          localChat = ChatManager.this.getThreadChat(localMessage.getThread());
          if (localChat == null)
            localChat = ChatManager.this.getUserChat(localMessage.getFrom());
        }
        if (localChat == null)
          localChat = ChatManager.this.createChat(localMessage);
        ChatManager.this.deliverMessage(localChat, localMessage);
      }
    }
    , local1);
  }

  public Chat createChat(String paramString, MessageListener paramMessageListener)
  {
    String str;
    do
      str = nextID();
    while (this.threadChats.get(str) != null);
    return createChat(paramString, str, paramMessageListener);
  }

  public Chat createChat(String paramString1, String paramString2, MessageListener paramMessageListener)
  {
    if (paramString2 == null)
      paramString2 = nextID();
    Chat localChat = (Chat)this.threadChats.get(paramString2);
    if (localChat != null)
      throw new IllegalArgumentException("ThreadID is already used");
    localChat = createChat(paramString1, paramString2, true);
    localChat.addMessageListener(paramMessageListener);
    return localChat;
  }

  private Chat createChat(String paramString1, String paramString2, boolean paramBoolean)
  {
    Chat localChat = new Chat(this, paramString1, paramString2);
    this.threadChats.put(paramString2, localChat);
    this.jidChats.put(paramString1, localChat);
    this.baseJidChats.put(StringUtils.parseBareAddress(paramString1), localChat);
    Iterator localIterator = this.chatManagerListeners.iterator();
    while (localIterator.hasNext())
    {
      ChatManagerListener localChatManagerListener = (ChatManagerListener)localIterator.next();
      localChatManagerListener.chatCreated(localChat, paramBoolean);
    }
    return localChat;
  }

  private Chat createChat(Message paramMessage)
  {
    String str1 = paramMessage.getThread();
    if (str1 == null)
      str1 = nextID();
    String str2 = paramMessage.getFrom();
    return createChat(str2, str1, false);
  }

  private Chat getUserChat(String paramString)
  {
    Chat localChat = (Chat)this.jidChats.get(paramString);
    if (localChat == null)
      localChat = (Chat)this.baseJidChats.get(StringUtils.parseBareAddress(paramString));
    return localChat;
  }

  public Chat getThreadChat(String paramString)
  {
    return (Chat)this.threadChats.get(paramString);
  }

  public void addChatListener(ChatManagerListener paramChatManagerListener)
  {
    this.chatManagerListeners.add(paramChatManagerListener);
  }

  public void removeChatListener(ChatManagerListener paramChatManagerListener)
  {
    this.chatManagerListeners.remove(paramChatManagerListener);
  }

  public Collection<ChatManagerListener> getChatListeners()
  {
    return Collections.unmodifiableCollection(this.chatManagerListeners);
  }

  private void deliverMessage(Chat paramChat, Message paramMessage)
  {
    paramChat.deliver(paramMessage);
  }

  void sendMessage(Chat paramChat, Message paramMessage)
  {
    Iterator localIterator = this.interceptors.entrySet().iterator();
    while (localIterator.hasNext())
    {
      Map.Entry localEntry = (Map.Entry)localIterator.next();
      PacketFilter localPacketFilter = (PacketFilter)localEntry.getValue();
      if ((localPacketFilter != null) && (localPacketFilter.accept(paramMessage)))
        ((PacketInterceptor)localEntry.getKey()).interceptPacket(paramMessage);
    }
    if (paramMessage.getFrom() == null)
      paramMessage.setFrom(this.connection.getUser());
    this.connection.sendPacket(paramMessage);
  }

  PacketCollector createPacketCollector(Chat paramChat)
  {
    return this.connection.createPacketCollector(new AndFilter(new PacketFilter[] { new ThreadFilter(paramChat.getThreadID()), new FromContainsFilter(paramChat.getParticipant()) }));
  }

  public void addOutgoingMessageInterceptor(PacketInterceptor paramPacketInterceptor)
  {
    addOutgoingMessageInterceptor(paramPacketInterceptor, null);
  }

  public void addOutgoingMessageInterceptor(PacketInterceptor paramPacketInterceptor, PacketFilter paramPacketFilter)
  {
    if (paramPacketInterceptor != null)
      this.interceptors.put(paramPacketInterceptor, paramPacketFilter);
  }
}

/* Location:           D:\hygieia\userclient\common\libs\easemobchat_2.1.8.jar
 * Qualified Name:     org.jivesoftware.smack.ChatManager
 * JD-Core Version:    0.6.1
 */